#include "recboxitem.h"
#include "ui_recboxitem.h"
#include "QPropertyAnimation"
#include<QDebug>
recBoxItem::recBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::recBoxItem)
{
    ui->setupUi(this);
    //监控对象安装事件处理器
    ui->musicImageBox->installEventFilter(this);
}

recBoxItem::~recBoxItem()
{
    delete ui;
}

bool recBoxItem::eventFilter(QObject *watched, QEvent *event)
{

    if(watched==ui->musicImageBox)
    {

        int width=ui->musicImageBox->width();
        int height=ui->musicImageBox->height();
        if(event->type()==QEvent::Enter)
        {

          QPropertyAnimation* animation=new QPropertyAnimation(ui->musicImageBox,"geometry");
          animation->setDuration(100);
          animation->setStartValue(QRect(0,0,width,height));
          animation->setEndValue(QRect(0,-6,width,height));
          animation->start();
          //动画结束发出finished信号,之后释放（申请的动画对象内存）
          connect(animation,&QPropertyAnimation::finished,this,[=](){
              delete animation;
              qDebug()<<"图⽚上移动画结束";
          });

          return true;
        }
        else if(event->type()==QEvent::Leave)
        {

            QPropertyAnimation* animation=new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(100);
            animation->setStartValue(QRect(0,-6,width,height));
            animation->setEndValue(QRect(0,0,width,height));
            animation->start();
            //动画结束发出finished信号,之后释放（申请的动画对象内存）
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
                qDebug()<<"图⽚上移动画结束";
            });

            return true;
        }
    }
        //其余情况执行父类的
        return QObject::eventFilter(watched, event);



}

void recBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void recBoxItem::setImage(const QString &Imagepath)
{
    QString imgStyle = "border-image:url("+Imagepath+");";
 ui->recMusicImage->setStyleSheet(imgStyle);
}
